using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Microsoft.WindowsMobile.PocketOutlook;
using Google.GData.AccessControl;
using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Contacts;
using Google.GData.Extensions;
using GSync;

using System.Diagnostics;
namespace GSync.GoogleSystem
{
    class GContact : GManager
    {
        #region Costanti
        /*
        public const int NOP_ITEM = 0;
        public const int INSERT_ITEM = 1;
        public const int EDITED_ITEM = 2;
        public const int DELETED_ITEM = 3;
        public const String CONTACT_ITEM = "CONATACT";
        public const String GOOGLE_CATEGORY = "GoogleImported";
        public const String OUTLOOK_IMPORTED = "OutlookImported:id:";
        public const String PRIMARY = "PRIMARY";
        public const String appService = "MrblueCo-GSync-01";
        */
        #endregion


        static GContact contactInstance;
        
       // private GCalendar gcal;

        public GContact(Config config) : base(config)
        {
            contactService = new ContactsService(appService);
            /*
            this.config = config;
            
            //MaxResults = config.MaxResults;
            gcal = new GCalendar(Config.Credential.UserName, Config.Credential.Password);
            addDefaultCal();
            contactService = new ContactsService(appService);
             */ 
        }

        public new static GContact getInstance(Config config)
        {
            if (contactInstance == null)
            {
                lock (syncRoot)
                {
                    if (contactInstance == null)
                        contactInstance = new GContact(config);
                }
            }
            return contactInstance;
        }


        /// <summary>
        /// Restituisce tutti i calendari di un utente
        /// </summary>
        /// <returns></returns>
        public ICollection getCalendars()
        {
            return gcal.RetriveUserCalendars();
        }


        /// <summary>
        /// Restituisce gli eventi di un calendario
        /// </summary>
        /// <param name="calName">Nome del calendario</param>
        /// <returns></returns>
        public ArrayList GetEvents(String calName)
        {
            return gcal.GetAllEventsByName(calName, config.Query, config.TextFilter);
        }
        /// <summary>
        /// Restituisce il feedUri dal nome del calednario
        /// </summary>
        /// <param name="gcalName"></param>
        /// <returns></returns>
        public String getFeedURI(String gcalName)
        {
            return gcal.getFeedURI(gcalName);
        }

        /// <summary>
        /// Restituisce gli eventi del calendario tramite feed URi
        /// </summary>
        /// <param name="FeedUriN"></param>
        /// <returns></returns>
        public ArrayList GetEventsByFeed(String FeedUriN)
        {
            return gcal.GetAllEvents(FeedUriN, config.Query, config.TextFilter);
        }


        /// <summary>
        /// Imposta i parameteri extre per la ricerca
        /// </summary>
        /// <param name="extra"></param>
        public void setExtraParam(String extra)
        {
            this.extraParam = extra;
        }

        public String getEventEntryToString(String title)
        {
            EventEntry evento = gcal.getEventEntry(title);
            String result = Properties.Resources.LABEL_TITLE + ":" +
                evento.Title.Text + "\r\n";
            if (evento.Summary.Text != null && evento.Summary.Text.Length > 0)
            {
                result += Properties.Resources.LABEL_BODY + ":" + evento.Summary.Text + "\r\n";
            }
            if (evento.Times.Count > 0)
            {
                result += Properties.Resources.LABEL_START + ":" + evento.Times[0].StartTime.ToString() + "\r\n" +
                    Properties.Resources.LABEL_END + ":" + evento.Times[0].EndTime.ToString() + "\r\n";
            }
            else
            {
                result += "Tutto il giorno" + "\r\n";
            }
            if (evento.Recurrence != null && evento.Recurrence.Value != null)
            {
                result += Properties.Resources.LABEL_RECURENT + ":" + evento.Recurrence.Value + "\r\n";
            }
            if (evento.Locations != null && evento.Locations.Count > 0)
            {
                foreach (Where whereItem in evento.Locations)
                {
                    result += Properties.Resources.LABEL_WHERE + ": " + whereItem.ValueString + "\r\n";
                }
            }
            return result;
        }

        public new String getEventNameByEventId(String eventId)
        {
            int last_pos = eventId.LastIndexOf("--(");
            return eventId.Substring(0, last_pos);
        }

        public void collectContact()
        {
            contactService.setUserCredentials(config.Credential.UserName, config.Credential.Password);
            ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
            query.ExtraParameters += "max-results=9999";
            try
            {
                ContactsFeed feed = contactService.Query(query);
                originalGContacts.Clear();
                foreach (ContactEntry entry in feed.Entries)
                {
                    Console.WriteLine(entry.Title.Text);
                    if (entry.Title.Text.Length > 0)
                    {
                        GSContact item = new GSContact();
                        item.setGoogle(entry);
                        item.Title = entry.Title.Text.ToString();
                        if (!originalGContacts.Contains(item))
                        {
                            originalGContacts.Add(item);
                        }
                    }
                }
                foreach (Contact contactO in getOutlookSession().Contacts.Items)
                {

                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }


    }
}
